package com.vancouver.knows.business.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vancouver.knows.business.bean.SearchPostInfo;

/**
 * 搜索列表解析
 * Created by llf
 */
public class SearchInfoHandle extends ACheckableJsonParser {

	public static final String HOT_KEYWORDS = "hot_keywords";
	public static final String KEY_POSTS = "post_result";
	public static final String KEY_EVENT_RESULT = "event_result";
	public static final String KEY_BUSINESS_RESULT = "business_result";
	public static final String KEY_USERPOST_RESULT = "userpost_result";
	
	public static final String SEARCH_TOTALCOUNT_POST = "search_totalcount_post";
	public static final String SEARCH_TOTALCOUNT_EVENT = "search_totalcount_event";
	public static final String SEARCH_TOTALCOUNT_BUSINESS = "search_totalcount_business";
	public static final String SEARCH_TOTALCOUNT_USERPOST = "search_totalcount_userpost";

	public static final String SEARCH_MAP = "search_map";
	public static final String SEARCH_POST_RESULT_COUNT = "search_post_result_count";
	public static final String SEARCH_EVENT_RESULT_COUNT = "search_event_result_count";
	public static final String SEARCH_BUSINESS_RESULT_COUNT =  "search_business_result_count";
	public static final String SEARCH_USERPOST_RESULT_COUNT = "search_userpost_result_count";
	public static final String SEARCH_TOTAL_TYPE = "search_total_type";
	
	private HashMap<String, Object> resultMap = new HashMap<String, Object>();

	@Override
	protected void parserContent(JSONObject content) throws Exception {
		int sizePostResult = 0;
		int sizeEventResult = 0;
		int sizeBusinessResult = 0;
		int sizeUserpostResult = 0;

		JSONArray hotKeywords = content.getJSONArray("hot_keywords");
		List<String> keywordStrings = new ArrayList<>();
		if (hotKeywords != null) {
			for (int i = 0; i < hotKeywords.size(); i++) {
				keywordStrings.add(hotKeywords.getString(i));
			}
			Collections.shuffle(keywordStrings);
			resultMap.put(HOT_KEYWORDS, keywordStrings);
		}

		//====================搜索城事数据部分=============================
		List<SearchPostInfo> postResultInfoList = new ArrayList<SearchPostInfo>();
		JSONObject jsonPostResult = content.getJSONObject("post_result");
		if (jsonPostResult != null) {
			resultMap.put(SEARCH_TOTALCOUNT_POST, Integer.valueOf(jsonPostResult.getString("total_count")));
			JSONArray array = jsonPostResult.getJSONArray("post_list");
			if (array == null || array.size() == 0){
				sizePostResult = 1;
				SearchPostInfo info = new SearchPostInfo();
				info.setId("1");
				info.setPostTitle(KEY_POSTS);
				info.setTitleType(KEY_POSTS);
				info.setSize(sizePostResult);//默认size = 1；
				postResultInfoList.add(info);
			}else{
				sizePostResult = array.size()+1;
				for(int i = 0; i < sizePostResult; i++){
					SearchPostInfo info = new SearchPostInfo();
					if (i == sizePostResult-1){
						info.setId("1");
						info.setPostTitle(KEY_POSTS);
						info.setTitleType(KEY_POSTS);
						info.setSize(sizePostResult);
					}else{
						info.setId(array.getJSONObject(i).getString("post_id"));
						info.setPostTitle(array.getJSONObject(i).getString("post_title"));
						info.setTitleType(KEY_POSTS);
						info.setSize(sizePostResult);
					}
					
					postResultInfoList.add(info);
				}
			}
		}else{
			resultMap.put(SEARCH_TOTALCOUNT_POST, 0);
		}
		
		//====================搜索活动数据部分=============================
		JSONObject jsonEventResult = content.getJSONObject("event_result");
		if (jsonEventResult != null) {
			resultMap.put(SEARCH_TOTALCOUNT_EVENT, Integer.valueOf(jsonEventResult.getString("total_count")));
			JSONArray array2 = jsonEventResult.getJSONArray("post_list");
			if (array2 == null || array2.size() == 0){
				sizeEventResult = 1;
				SearchPostInfo info = new SearchPostInfo();
				info.setId("2");
				info.setPostTitle(KEY_EVENT_RESULT);
				info.setTitleType(KEY_EVENT_RESULT);
				info.setSize(sizeEventResult);//默认size = 1；
				postResultInfoList.add(info);
			}else{
				sizeEventResult = array2.size()+1;
				for(int i = 0; i < sizeEventResult;i++){
					SearchPostInfo info = new SearchPostInfo();
					if (i == sizeEventResult-1){
						info.setId("2");
						info.setPostTitle(KEY_EVENT_RESULT);
						info.setTitleType(KEY_EVENT_RESULT);
						info.setSize(sizeEventResult);
					}else{
						info.setId(array2.getJSONObject(i).getString("post_id"));
						info.setPostTitle(array2.getJSONObject(i).getString("post_title"));
						info.setTitleType(KEY_EVENT_RESULT);
						info.setSize(sizeEventResult);
					}
					
					postResultInfoList.add(info);
				}
			}
		}else{
			resultMap.put(SEARCH_TOTALCOUNT_EVENT, 0);
		}
		
		//====================搜索诚惠数据部分=============================
		JSONObject jsonBusinessResult = content.getJSONObject("business_result");
		if (jsonBusinessResult != null) {
			resultMap.put(SEARCH_TOTALCOUNT_BUSINESS, Integer.valueOf(jsonBusinessResult.getString("total_count")));
			JSONArray array3 = jsonBusinessResult.getJSONArray("post_list");
			if (array3 == null || array3.size() == 0){
				sizeBusinessResult = 1;
				SearchPostInfo info = new SearchPostInfo();
				info.setId("3");
				info.setPostTitle(KEY_BUSINESS_RESULT);
				info.setTitleType(KEY_BUSINESS_RESULT);
				info.setSize(sizeBusinessResult);//默认size = 1；
				postResultInfoList.add(info);
			}else{
				sizeBusinessResult = array3.size()+1;
				for(int i = 0; i < sizeBusinessResult;i++){
					SearchPostInfo info = new SearchPostInfo();
					if (i == sizeBusinessResult-1){
						info.setId("3");
						info.setPostTitle(KEY_BUSINESS_RESULT);
						info.setTitleType(KEY_BUSINESS_RESULT);
						info.setSize(sizeBusinessResult);
					}else{
						info.setId(array3.getJSONObject(i).getString("post_id"));
						info.setPostTitle(array3.getJSONObject(i).getString("post_title"));
						info.setTitleType(KEY_BUSINESS_RESULT);
						info.setSize(sizeBusinessResult);
					}
					
					postResultInfoList.add(info);
				}
			}
		}else{
			resultMap.put(SEARCH_TOTALCOUNT_BUSINESS, 0);
		}

		//==================== user post results =============================
		JSONObject jsonUserpostResult = content.getJSONObject("userpost_result");
		if (jsonUserpostResult != null) {
			resultMap.put(SEARCH_TOTALCOUNT_USERPOST, Integer.valueOf(jsonUserpostResult.getString("total_count")));
			JSONArray array4 = jsonUserpostResult.getJSONArray("post_list");
			if (array4 == null || array4.size() == 0) {
				sizeUserpostResult = 1;
				SearchPostInfo info = new SearchPostInfo();
				info.setId("4");
				info.setPostTitle(KEY_USERPOST_RESULT);
				info.setTitleType(KEY_USERPOST_RESULT);
				info.setSize(sizeUserpostResult);
				postResultInfoList.add(info);
			} else {
				sizeUserpostResult = array4.size() + 1;
				for (int i = 0; i < sizeUserpostResult; i++) {
					SearchPostInfo info = new SearchPostInfo();
					if (i == sizeUserpostResult - 1) {
						info.setId("4");
						info.setPostTitle(KEY_USERPOST_RESULT);
						info.setTitleType(KEY_USERPOST_RESULT);
						info.setSize(sizeUserpostResult);
					} else {
						info.setId(array4.getJSONObject(i).getString("post_id"));
						info.setPostTitle(array4.getJSONObject(i).getString("post_title"));
						info.setTitleType(KEY_USERPOST_RESULT);
						info.setSize(sizeUserpostResult);
					}

					postResultInfoList.add(info);
				}
			}
		} else {
			resultMap.put(SEARCH_TOTALCOUNT_USERPOST, 0);
		}

		resultMap.put(SEARCH_MAP, postResultInfoList);
		resultMap.put(SEARCH_POST_RESULT_COUNT, sizePostResult);
		resultMap.put(SEARCH_EVENT_RESULT_COUNT, sizeEventResult);
		resultMap.put(SEARCH_BUSINESS_RESULT_COUNT, sizeBusinessResult);
		resultMap.put(SEARCH_USERPOST_RESULT_COUNT, sizeUserpostResult);
	}
	
	

	public HashMap<String, Object> getResultMap() {
		return resultMap;
	}

}
